﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Web;
using Models;

namespace DAL
{
    public class CustomerServices
    {
        /// <summary>
        /// 修改客户
        /// </summary>
        /// <param name="c">客户实体</param>
        /// <returns>成功与否</returns>
        public static bool UpdateCustomer(Customer c)
        {
            string sql = $"update Customer set LoginID='{c.LoginID}',Pwd='{c.Pwd}',CustomerName='{c.CustomerName}',IdentityID='{c.IdentityID}',Address='{c.Address}',Phone='{c.Phone}' where CustomerID={c.CustomerID}";
            return DBHelper.ExecuteSql(sql);
        }

        /// <summary>
        /// 通过ID查询客户信息
        /// </summary>
        /// <param name="cid">客户ID</param>
        /// <returns>客户实体</returns>
        public static Customer SearchCustomersByID(string cid)
        {
            Customer c = null;
            DataTable dt = DBHelper.GetDataTable($"select * from Customer where CustomerID={cid}");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                c = new Customer();
                c.CustomerID = dt.Rows[i]["CustomerID"].ToString();
                c.LoginID = dt.Rows[i]["LoginID"].ToString();
                c.Pwd = dt.Rows[i]["Pwd"].ToString();
                c.CustomerName = dt.Rows[i]["CustomerName"].ToString();
                c.IdentityID = dt.Rows[i]["IdentityID"].ToString();
                c.Address = dt.Rows[i]["Address"].ToString();
                c.Phone = dt.Rows[i]["Phone"].ToString();
            }
            return c;
        }

        /// <summary>
        /// 通过姓名搜索客户
        /// </summary>
        /// <param name="name">姓名</param>
        /// <returns>客户列表</returns>
        public static List<Customer> SearchCustomersByName(string name)
        {
            List<Customer> list = new List<Customer>();
            DataTable dt = DBHelper.GetDataTable($"select * from Customer where CustomerName like '%{name}%'");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Customer c = new Customer();
                c.CustomerID = dt.Rows[i]["CustomerID"].ToString();
                c.LoginID = dt.Rows[i]["LoginID"].ToString();
                c.Pwd = dt.Rows[i]["Pwd"].ToString();
                c.CustomerName = dt.Rows[i]["CustomerName"].ToString();
                c.IdentityID = dt.Rows[i]["IdentityID"].ToString();
                c.Address = dt.Rows[i]["Address"].ToString();
                c.Phone = dt.Rows[i]["Phone"].ToString();
                list.Add(c);
            }
            return list;
        }

        /// <summary>
        /// 添加客户
        /// </summary>
        /// <param name="c">客户实体</param>
        /// <returns>成功与否</returns>
        public static bool AddCustomer(Customer c)
        {
            string sql = $"insert Customer values('{c.LoginID}','{c.Pwd}','{c.CustomerName}','{c.IdentityID}','{c.Address}','{c.Phone}')";
            return DBHelper.ExecuteSql(sql);
        }

        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="cid">客户ID</param>
        /// <returns>成功与否</returns>
        public static bool DeleteCustomerByID(string cid)
        {
            return DBHelper.ExecuteSql($"delete Customer where CustomerID={cid}");
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns>用户列表</returns>
        public static List<Customer> GetCustomers()
        {
            List<Customer> list = new List<Customer>();
            DataTable dt = DBHelper.GetDataTable("select * from Customer");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Customer c = new Customer();
                c.CustomerID = dt.Rows[i]["CustomerID"].ToString();
                c.LoginID = dt.Rows[i]["LoginID"].ToString();
                c.Pwd = dt.Rows[i]["Pwd"].ToString();
                c.CustomerName = dt.Rows[i]["CustomerName"].ToString();
                c.IdentityID = dt.Rows[i]["IdentityID"].ToString();
                c.Address = dt.Rows[i]["Address"].ToString();
                c.Phone = dt.Rows[i]["Phone"].ToString();
                list.Add(c);
            }
            return list;
        }
    }
}